/**
 * 
 */
package com.hand.cloud.b2b.daos.impl;

import de.hybris.platform.servicelayer.exceptions.ModelSavingException;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hand.cloud.b2b.daos.TBProductDAO;
import com.hand.cloud.b2b.enums.EPProductStuffStatus;
import com.hand.cloud.b2b.enums.EPProductType;
import com.hand.cloud.b2b.model.EPProductModel;
import com.hand.cloud.b2b.model.TB_ProductModel;


/**
 * @author StephenWang
 * 
 */
@Component(value = "tbProductDAO")
public class DefaultTBProductDAO implements TBProductDAO
{

	@Autowired
	private ModelService modelService;

	@Autowired
	private FlexibleSearchService flexibleSearchService;

	private final Logger LOG = Logger.getLogger(DefaultTBProductDAO.class);

	/**
	 * 创建商品
	 * 
	 * @param product
	 * @return
	 */
	@Override
	public TB_ProductModel createTBProduct(final EPProductModel product)
	{
		final TB_ProductModel tbProduct = this.modelService.create(TB_ProductModel.class);

		tbProduct.setApprove_status(String.valueOf(product.getApproveStatus()));
		//tbProduct.setApprove_status(product.getApprove_status().toString()); 
		tbProduct.setCid(product.getCid());
		tbProduct.setDesc(product.getDesc());
		//tbProduct.setT_location_city(product.getLocation_city());
		tbProduct.setT_location_city(product.getLocationCity());
		//tbProduct.setT_location_state(product.getLocation_state());
		tbProduct.setT_location_state(product.getLocationState());
		tbProduct.setNum(product.getNum());
		tbProduct.setPrice(product.getPrice());
		tbProduct.setStuff_status(String.valueOf(product.getStuffStatus()));
		//tbProduct.setStuff_status(product.getStuff_status().toString()); 
		tbProduct.setTitle(product.getTitle());
		tbProduct.setType(String.valueOf(product.getType()));
		//tbProduct.setOuter_id(product.getSource_pk());
		tbProduct.setOuter_id(product.getSourceProduct().getPk().toString());

		try
		{
			this.modelService.save(tbProduct);
		}
		catch (final ModelSavingException e)
		{
			e.printStackTrace();
			return null;
		}

		return tbProduct;
	}

	/**
	 * 更新TB商品信息
	 */
	@Override
	public TB_ProductModel updateTBProduct(final EPProductModel epProduct)
	{
		TB_ProductModel tbProduct = new TB_ProductModel();
		tbProduct.setSourceProduct(epProduct);

		try
		{
			tbProduct = this.flexibleSearchService.getModelByExample(tbProduct);
			LOG.info("-----EpProdcutCode:" + epProduct.getCode());

			tbProduct.setNum(epProduct.getNum());
			tbProduct.setPrice(epProduct.getPrice());
			tbProduct.setType(EPProductType.FIXED.toString());
			tbProduct.setStuff_status(EPProductStuffStatus.NEW.toString());
			tbProduct.setTitle(epProduct.getTitle());
			tbProduct.setDesc(epProduct.getDesc());
			tbProduct.setCid(epProduct.getCid());
			tbProduct.setT_location_city(epProduct.getLocationCity());
			tbProduct.setT_location_state(epProduct.getLocationState());
		}
		catch (final Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return tbProduct;
	}

}
